package com.lsy.android.pingpang.tool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Tool {

	public static byte[] ObjectToByte(Object obj) {
		byte[] bytes = null;
		ByteArrayOutputStream bo = null;
		ObjectOutputStream oo = null;
		try {
			bo = new ByteArrayOutputStream();
			oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bo != null) {
					bo.close();
					bo = null;
				}
				if (oo != null) {
					oo.close();
					oo = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}

	public static Object ByteToObject(byte[] bytes) {
		Object obj = null;
		ByteArrayInputStream bis = null;
		ObjectInputStream ois = null;
		try {
			bis = new ByteArrayInputStream(bytes);
			ois = new ObjectInputStream(bis);
			obj = ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null) {
					bis.close();
					bis = null;
				}
				if (ois != null) {
					ois.close();
					ois = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

}
